<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属公司" prop="provideGroup">
        <el-select v-model="queryParams.company" filterable placeholder="请选择公司" clearable style="width: 150px;">
          <el-option v-for="item in companyType" :key="item.label" :label="item.value" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="provide">
        <el-input v-model="queryParams.provide" placeholder="请输入供应商" style="width: 150px;" />
      </el-form-item>
      <el-form-item label="仓库" prop="provideWare">
        <el-select v-model="queryParams.provideWare" filterable placeholder="请输入仓库" clearable style="width: 150px;">
          <el-option v-for="item in provideWare" :key="item.label" :label="item.value" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="种类" prop="provideGroup">
        <el-select v-model="queryParams.provideGroup" filterable placeholder="请选择种类" clearable style="width: 150px;">
          <el-option v-for="item in provideGroup" :key="item.label" :label="item.value" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开票类型" prop="provideGroup">
        <el-select v-model="queryParams.provideType" filterable placeholder="请选择种类" clearable style="width: 150px;">
          <el-option v-for="item in provideType" :key="item.label" :label="item.value" :value="item.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开票时间" prop="warehouseName">
        <el-date-picker clearable v-model="dateTime" style="width:335px" type="datetimerange" start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择开票时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:SuppliesInvoiceList:Export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table v-loading="loading" :data="BillingList" border class="table">
      <el-table-column prop="index" label="序号" width="55" align="center"></el-table-column>
      <el-table-column label="开票时间" prop="invoiceTime" align="center" width="200px">
      </el-table-column>
      <el-table-column label="供应商" prop="provide" align="center">
      </el-table-column>
      <el-table-column prop="provideWare" label="仓库" width="180" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.provideWare === '中心库'" class="DataCenter">
            <el-image :src="zxkImg" class="zxkImg"></el-image>
            <span class="text">{{ scope.row.provideWare }}</span>
          </div>
          <div v-if="scope.row.provideWare === '配件库'" class="DataCenter">
            <span class="text1">{{ scope.row.provideWare }}</span>
          </div>

        </template>
      </el-table-column>
      <el-table-column prop="provideType" label="类型" width="180" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.provideType === '开票'">{{ scope.row.provideType }}</span>
          <span v-if="scope.row.provideType === '入账'"> <i class="el-icon-notebook-2"></i> {{ scope.row.provideType
            }}</span>
          <div v-if="scope.row.provideType === '作废'" class="DataCenter">
            <el-image :src="zffpImg" class="zffpImg"></el-image>
            <span class="text1">{{ scope.row.provideType }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="provideGroup" label="种类" width="180" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.provideGroup === '配件'" class="DataCenter">
            <el-image :src="pjImg" class="zxkImg"></el-image>
            <span class="text1">{{ scope.row.provideGroup }}</span>
          </div>
          <div v-if="scope.row.provideGroup === '轮胎'" class="DataCenter">
            <el-image :src="ltImg" class="pjkImg"></el-image>
            <span class="text1">{{ scope.row.provideGroup }}</span>
          </div>
          <div v-if="scope.row.provideGroup === '制冷剂'" class="DataCenter">
            <el-image :src="zljImg" class="zxkImg"></el-image>
            <span class="text1">{{ scope.row.provideGroup }}</span>
          </div>
          <div v-if="scope.row.provideGroup === '润滑油'" class="DataCenter">
            <el-image :src="yhyImg" class="pjkImg"></el-image>
            <span class="text1">{{ scope.row.provideGroup }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" prop="company" align="center">
      </el-table-column>
      <el-table-column prop="provideMoney" label="金额 (元) " align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.provideType === '开票'" style="float: right;">{{
      formatNumber(Math.abs(parseFloat(scope.row.provideMoney)).toFixed(2)) }}</span>
          <span v-if="scope.row.provideType === '作废'" style="float: right;color:red">-{{
      formatNumber(parseFloat(scope.row.provideMoney).toFixed(2)) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" width="350">
        <template slot-scope="scope" >
          <div class="DataCenter">
             <span class="text2" v-if="scope.row.provideType === '开票'">开票编码:{{ scope.row.remark }} ({{ scope.row.provideWare }}{{
      scope.row.provideGroup }})</span>
            <span  class="text2" v-if="scope.row.provideType === '作废'">开票编码:{{ scope.row.remark }} ({{ scope.row.provideWare }}{{
      scope.row.provideGroup }})-{{ scope.row.provideType }}</span>
            <span  class="text2" v-if="scope.row.provideType === '入账'">{{ scope.row.remark }}</span>
            <el-image v-if="scope.row.status === '1'" :src="zffpImg" class="zffpImg"></el-image>

          </div>

        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" prop="productGroup" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.provideType === '开票' && scope.row.status === null" size="mini" type="text"
            @click="cellClick(scope.row)">发票作废</el-button>
          <span v-if="scope.row.status == '1'" class=" disabled-text">已作废</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="NumAll">


      <div class="textNuM1" v-if="this.Summoney">总金额：<span class="kpNum"> {{ formatNumber(this.Summoney) }}</span>
      </div>
      <div class="textNuM1" v-if="this.Pcount > 0">开票总数：<span class="kpNum"> {{
      this.Pcount
    }}</span>
      </div>
      <div class="textNuM1" v-if="this.Zcount > 0">作废总数：<span class="kpNum"> {{
      this.Zcount
    }}</span></div>
    </div>
    <pagination v-show="total > 0" layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page.sync="queryParams.pageNum" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { detailList1, invoiceBack } from "@/api/system/supplierBillingDetail";
export default {
  name: "SuppliesInvoiceList",
  data() {
    return {
      zxkImg: require('../../../assets/icon/zxk.png'),
      pjImg: require('../../../assets/icon/pj.png'),
      ltImg: require('../../../assets/icon/lt.png'),
      zljImg: require('../../../assets/icon/zlj.png'),
      yhyImg: require('../../../assets/icon/yhy.png'),
      zffpImg: require('../../../assets/icon/zffp.png'),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商付款表格数据
      BillingList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provide: "",
        provideType: "",
        provideGroup: "",
        provideWare: "",
        startTime: "",
        endTime: "",
        company: ""
      },
      deptName: "",
      dateTime: [this.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), 1, 0, 0, 0, 0)), this.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 23, 59, 59, 999))],
      provideGroup: [
        { value: '润滑油', label: '润滑油' },
        { value: '配件', label: '配件' },
        { value: '轮胎', label: '轮胎' },
        { value: '制冷剂', label: '制冷剂' }
      ],
      provideWare: [
        { value: '中心库', label: '中心库' },
        { value: '配件库', label: '配件库' },
      ],
      provideType: [
        { value: '开票', label: '开票' },
        { value: '作废', label: '作废' },
      ],
      companyType: [
        { value: '营运公司', label: '营运公司' },
        { value: '金山巴士', label: '金山巴士' },
        { value: '崇明巴士', label: '崇明巴士' }
      ],
      Summoney: 0,
      Pcount: 0,
      Zcount: 0,
      // fisrtActivate: true
    };
  },
  created() {
    this.eventHnadling();
  },
  // activated() {
  //   if (!this.fisrtActivate) {
  //     this.eventHnadling();
  //   } else {
  //     // 防止 重复请求
  //     this.fisrtActivate = false;
  //   }
  // },
  methods: {

    eventHnadling() {
      const User = localStorage.getItem('User');
      this.deptName = JSON.parse(User).dept ? JSON.parse(User).dept.deptName : '';
      if (this.deptName !== '物资公司') {
        this.queryParams.provide = this.deptName;
      } else {
        this.queryParams.provide = "";
      }
      this.getList();
    },
    /** 查询供应商付款列表 */
    getList() {
      if (this.dateTime && this.dateTime.length === 2) {
        // 日期格式转换
        this.queryParams.startTime = this.dateTime[0];
        this.queryParams.endTime = this.dateTime[1];
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      this.loading = true;

      detailList1(this.queryParams).then(response => {
        if (response.code === 200) {
          this.BillingList = response.data.list;
          this.BillingList.forEach((item, index) => {
            item.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
          });
          this.Summoney = parseFloat(localStorage.getItem('summoney')).toFixed(2);
          this.Pcount = parseFloat(localStorage.getItem('pcount'));
          this.Zcount = parseFloat(localStorage.getItem('zcount'));
          this.total = response.data.total;
        }
        this.loading = false;
      });
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.provide = "";
      this.queryParams.provideType = "";
      this.queryParams.company = "";
      this.dateTime = [this.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), 1, 0, 0, 0, 0)), this.formatDate(new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 23, 59, 59, 999))];
      this.handleQuery();
    },

    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('system/detail/export', {
        ...this.queryParams
      }, `物资开票清单${this.queryParams.endTime.split(' ')[0]}.xlsx`)
    },
    // 数字转换
    formatNumber(value) {
      if (value) {
        let [integerPart, decimalPart] = value.toString().split('.');
        let formattedNumber = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + '.' + decimalPart.slice(0, 3);
        return formattedNumber;
      }

    },
    // 日期转换
    formatDate(date) {
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const hours = date.getHours().toString().padStart(2, '0');
      const minutes = date.getMinutes().toString().padStart(2, '0');
      const seconds = date.getSeconds().toString().padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    // 发票作废
    cellClick(row) {
      this.$confirm('是否作废此条发票?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        showClose: false,
        closeOnClickModal: false
      }).then(() => {
        let params = {
          id: row.id,
          money: Math.abs(parseFloat(row.provideMoney)),
          productGroup: row.provideGroup,
          provide: row.provide,
          remark: row.remark,
          productWare: row.provideWare,
          company: row.company
        }
        invoiceBack(params).then(rst => {
          if (rst.code === 200) {
            this.$message({
              type: 'success',
              message: '作废成功!'
            });
            this.getList();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消作废'
        });
      });
    }
  }

};
</script>
<style lang="scss" scoped>
.textNum {
  float: right;
  color: red;
}

.textNum:hover {
  color: #409EFF;
}

.table {
  .el-table__body-wrapper {
    // height: 650px !important;
    overflow-y: auto !important;
  }
}

.NumAll {
  position: absolute;
  z-index: 999;
  margin-top: 10px;
  display: flex;

  .textNuM1 {
    margin-right: 15px;
    font-size: 15px;
    margin-top: 3px;
    color: #c0c4cc;

    .kpNum {
      font-weight: 600;
      color: red;
    }
  }
}

.disabled-text {
  font-size: 12px;
  color: #C0C4CC
}

.disabled-text:hover {
  cursor: not-allowed;
  /* 其他样式，比如改变文本颜色等 */
}

.zxkImg {
  width: 30px;
  height: 30px;
}

.DataCenter {
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  margin-top: 10px;
}

.pjkImg {
  width: 28px;
  height: 28px;
}

.text1 {
  margin-top: 2px;
  margin-left: 5px;
}
.text2{
  margin-top: 2px;
  margin-right: 5px;
}

.zffpImg {
  width: 17px;
  height: 17px;
}
</style>
